package com.shse.web.controller;

import com.shse.meta.Article;
import com.shse.meta.User;
import com.shse.service.ArticleService;
import com.shse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

/**
 * Created by shse on 3/12/2016.
 */
@Controller
public class HelloController {
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/")
    public String index(){
        return "redirect:/article";
    }

    @RequestMapping("/list")
    public String list(ModelMap map){

        List<User> userList = userService.getAllUser();
        map.put("userList", userList);
        return "list";
    }

    @RequestMapping("/user/{id}")
    public String getUser(@PathVariable String id, ModelMap map){

        User user = userService.getUserById(id);
        map.put("user", user);
        return "user";

    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            HttpSession session,
                            ModelMap map){

        User user = userService.login(username, password);
        if(user!=null){
            map.put("user", user);
            session.setAttribute("user", user);

            return "redirect:/";
        }else{
            map.put("error", "用户名或者密码错误");
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user!=null){
            session.removeAttribute("user");
        }
        return "redirect:/";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    @RequestMapping("/article")
    public String article(@RequestParam(value = "user", required = false) String user,
                          ModelMap map,
                          HttpSession session){
        List<Article> articles  = null;
        if(user!=null && !user.trim().equals("")){
            articles = articleService.getArticlesByAuthor(user);
        }else{
            articles = articleService.getAllArticle();
        }
        User loggedUser = (User) session.getAttribute("user");
        if(loggedUser!=null){
            map.put("user", loggedUser);
        }
        map.put("articles", articles);
        return "articles";
    }

    @RequestMapping(value = "/article/{id}", method = RequestMethod.GET)
    public String article(ModelMap map,
                          @PathVariable String id,
                          @RequestParam(value = "method", required = false) String method,
                          HttpSession session){
        Article article = articleService.getArticleById(id);
        map.put("article", article);

        User user = (User) session.getAttribute("user");
        if(method!=null && method.equals("update") && user!=null){
            return "articleUpdate";
        }

        return "article";
    }

    @RequestMapping(value = "/article/{id}", method = RequestMethod.POST)
    public String article(@PathVariable String id,
                          @RequestParam("title") String title,
                          @RequestParam("content") String content){

        articleService.updateArticle(id, title, content);

        return "redirect:/article";
    }
}
